﻿using System;
using System.Linq;
using System.Linq.Expressions;
using System.Linq.Dynamic;
using System.Text;
using System.Data.Linq;
using System.Transactions;
using LinqToSqlExtensions;
using System.ServiceModel;
using System.Collections;
using System.Collections.Generic;
using CardsServer.DAL;

namespace CardsServer.WCF
{
    public partial interface IService
    {
        [OperationContract]
        string KCSale_M_Add(KCSale_M model, int ACID);


        [OperationContract]
        string KCSale_M_Update(KCSale_M model, int ACID);


        [OperationContract]
        string KCSale_MBypid_Delete(string pid, int ACID);


        [OperationContract]
        KCSale_M KCSale_MBypid_Get(string pid, int ACID);


        [OperationContract]
        List<KCSale_M> KCSale_M_Get(string sortBy, int ACID);




    }
    public partial class Service : IService
    {
        #region  添加KCSale_M表数据
        /// <summary>
        /// 添加KCSale_M
        /// </summary>
        /// <param name="model">KCSale_M实体</param>
        /// <returns>空或错误</returns>
        public string KCSale_M_Add(KCSale_M model, int ACID)
        {
            if (string.IsNullOrEmpty(model.pid))
            {
                return "『单据号』不能为空！";
            }
            if (model.isfinish == null)
            {
                return "『付清标志』不能为空！";
            }
            if (model.ischeck == null)
            {
                return "『复核标志』不能为空！";
            }
            if (model.discount == null)
            {
                return "『折让金额』不能为空！";
            }
            if (model.balance == null)
            {
                return "『余额』不能为空！";
            }
            if (model.total == null)
            {
                return "『总金额』不能为空！";
            }
            if (model.ClientID == null || model.ClientID.Equals(Guid.Empty))
            {
                return "『供应商』不能为空！";
            }
            if (string.IsNullOrEmpty(model.WarehouseID))
            {
                return "『仓库ID』不能为空！";
            }
            if (model.opID == null || model.opID.Equals(Guid.Empty))
            {
                return "『开单员』不能为空！";
            }
            if (model.Pdate == null)
            {
                return "『开单日期』不能为空！";
            }
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                try
                {
                    if (ctx.KCSale_M.Count(p => p.pid == model.pid) > 0)
                    {
                        return "主键已存在！";
                    }
                    ctx.KCSale_M.InsertOnSubmit(model);
                    ctx.SubmitChanges();
                    return string.Empty;
                }
                catch (Exception ex)
                {
                    return " 添加失败!错误详细信息：" + ex.Message;
                }
            }
        }
        #endregion

        #region  添加KCSale_M表数据事物用重构
        /// <summary>
        /// 添加KCSale_M事物用重构
        /// </summary>
        /// <param name="model">KCSale_M实体</param>
        /// <returns>空或错误</returns>
        private string KCSale_M_Add(KCSale_M model, int ACID, CardsServer.DAL.DataClassesDataContext ctx)
        {
            if (string.IsNullOrEmpty(model.pid))
            {
                return "『单据号』不能为空！";
            }
            if (model.isfinish == null)
            {
                return "『付清标志』不能为空！";
            }
            if (model.ischeck == null)
            {
                return "『复核标志』不能为空！";
            }
            if (model.discount == null)
            {
                return "『折让金额』不能为空！";
            }
            if (model.balance == null)
            {
                return "『余额』不能为空！";
            }
            if (model.total == null)
            {
                return "『总金额』不能为空！";
            }
            if (model.ClientID == null || model.ClientID.Equals(Guid.Empty))
            {
                return "『供应商』不能为空！";
            }
            if (string.IsNullOrEmpty(model.WarehouseID))
            {
                return "『仓库ID』不能为空！";
            }
            if (model.opID == null || model.opID.Equals(Guid.Empty))
            {
                return "『开单员』不能为空！";
            }
            if (model.Pdate == null)
            {
                return "『开单日期』不能为空！";
            }
            if (ctx.KCSale_M.Count(p => p.pid == model.pid) > 0)
            {
                return "主键已存在！";
            }
            ctx.KCSale_M.InsertOnSubmit(model);
            return string.Empty;
        }
        #endregion



        #region  修改KCSale_M表数据
        /// <summary>
        /// 修改KCSale_M
        /// </summary>
        /// <param name="model">KCSale_M实体</param>
        /// <returns>空或错误</returns>
        public string KCSale_M_Update(KCSale_M model, int ACID)
        {
            if (string.IsNullOrEmpty(model.pid))
            {
                return "『单据号』不能为空！";
            }
            if (model.isfinish == null)
            {
                return "『付清标志』不能为空！";
            }
            if (model.ischeck == null)
            {
                return "『复核标志』不能为空！";
            }
            if (model.discount == null)
            {
                return "『折让金额』不能为空！";
            }
            if (model.balance == null)
            {
                return "『余额』不能为空！";
            }
            if (model.total == null)
            {
                return "『总金额』不能为空！";
            }
            if (model.ClientID == null || model.ClientID.Equals(Guid.Empty))
            {
                return "『供应商』不能为空！";
            }
            if (string.IsNullOrEmpty(model.WarehouseID))
            {
                return "『仓库ID』不能为空！";
            }
            if (model.opID == null || model.opID.Equals(Guid.Empty))
            {
                return "『开单员』不能为空！";
            }
            if (model.Pdate == null)
            {
                return "『开单日期』不能为空！";
            }
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                try
                {
                    if (ctx.KCSale_M.Count(p => p.pid == model.pid) != 1)
                    {
                        return "未找到指定记录或记录不唯一！";
                    }
                    KCSale_M KCSale_Mmodel = ctx.KCSale_M.Single(p => p.pid == model.pid);
                    if (!string.IsNullOrEmpty(model.pid))
                    {
                        KCSale_Mmodel.pid = model.pid;
                    }
                    if (!string.IsNullOrEmpty(model.Remarks))
                    {
                        KCSale_Mmodel.Remarks = model.Remarks;
                    }
                    if (model.Pdate != null)
                    {
                        KCSale_Mmodel.Pdate = model.Pdate;
                    }
                    if (model.opID != null || model.opID.Equals(Guid.Empty))
                    {
                        KCSale_Mmodel.opID = model.opID;
                    }
                    if (!string.IsNullOrEmpty(model.WarehouseID))
                    {
                        KCSale_Mmodel.WarehouseID = model.WarehouseID;
                    }
                    if (!string.IsNullOrEmpty(model.orderid))
                    {
                        KCSale_Mmodel.orderid = model.orderid;
                    }
                    if (model.ClientID != null || model.ClientID.Equals(Guid.Empty))
                    {
                        KCSale_Mmodel.ClientID = model.ClientID;
                    }
                    if (!string.IsNullOrEmpty(model.linkman))
                    {
                        KCSale_Mmodel.linkman = model.linkman;
                    }
                    if (!string.IsNullOrEmpty(model.tel))
                    {
                        KCSale_Mmodel.tel = model.tel;
                    }
                    if (model.total != null)
                    {
                        KCSale_Mmodel.total = model.total;
                    }
                    if (model.balance != null)
                    {
                        KCSale_Mmodel.balance = model.balance;
                    }
                    if (model.discount != null)
                    {
                        KCSale_Mmodel.discount = model.discount;
                    }
                    if (model.JSempid != null || model.JSempid.Equals(Guid.Empty))
                    {
                        KCSale_Mmodel.JSempid = model.JSempid;
                    }
                    if (model.ischeck != null)
                    {
                        KCSale_Mmodel.ischeck = model.ischeck;
                    }
                    if (model.Checkempid != null || model.Checkempid.Equals(Guid.Empty))
                    {
                        KCSale_Mmodel.Checkempid = model.Checkempid;
                    }
                    if (model.isfinish != null)
                    {
                        KCSale_Mmodel.isfinish = model.isfinish;
                    }
                    ctx.SubmitChanges();
                    return string.Empty;
                }
                catch (Exception ex)
                {
                    return " 修改失败!错误详细信息：" + ex.Message;
                }
            }
        }
        #endregion



        #region  删除KCSale_M表Bypid数据
        /// <summary>
        /// 删除KCSale_MBypid
        /// </summary>
        /// <param name="pid">单据号</param>
        /// <returns>空或错误</returns>
        public string KCSale_MBypid_Delete(string pid, int ACID)
        {
            if (string.IsNullOrEmpty(pid))
            {
                return "『单据号』不能为空！";
            }
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                try
                {
                    if (ctx.KCSale_M.Count(p => p.pid == pid) != 1)
                    {
                        return null;
                    }
                    KCSale_M model = ctx.KCSale_M.Single(p => p.pid == pid);
                    ctx.KCSale_M.DeleteOnSubmit(model);
                    ctx.SubmitChanges();
                    return string.Empty;
                }
                catch (Exception ex)
                {
                    return " 删除失败!错误详细信息：" + ex.Message;
                }
            }
        }
        #endregion

        #region  删除KCSale_M表Bypid数据[事物用重构]
        /// <summary>
        /// 删除KCSale_MBypid[事物用重构]
        /// </summary>
        /// <param name="pid">单据号</param>
        /// <returns>空或错误</returns>
        private string KCSale_MBypid_Delete(string pid, int ACID, CardsServer.DAL.DataClassesDataContext ctx)
        {
            if (string.IsNullOrEmpty(pid))
            {
                return "『单据号』不能为空！";
            }
            if (ctx.KCSale_M.Count(p => p.pid == pid) != 1)
            {
                return null;
            }
            KCSale_M model = ctx.KCSale_M.Single(p => p.pid == pid);
            ctx.KCSale_M.DeleteOnSubmit(model);
            return string.Empty;
        }
        #endregion



        #region  获取KCSale_M表Bypid数据
        /// <summary>
        /// 获取KCSale_MBypid
        /// </summary>
        /// <param name="pid">单据号</param>
        /// <returns>当前表</returns>
        public KCSale_M KCSale_MBypid_Get(string pid, int ACID)
        {
            if (string.IsNullOrEmpty(pid))
            {
                return null;
            }
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                try
                {
                    if (ctx.KCSale_M.Count(p => p.pid == pid) != 1)
                    {
                        return null;
                    }
                    return ctx.KCSale_M.SingleOrDefault(p => p.pid == pid);
                }
                catch (Exception ex)
                {
                    return null;
                }
            }
        }
        #endregion



        #region  获取KCSale_M表数据
        /// <summary>
        /// 获取KCSale_M
        /// </summary>
        /// <param name="sortBy">排序属性名</param>
        /// <param name="sortType">排序类型0=升序 1=降序</param>
        /// <returns>List<KCSale_M></returns>
        public List<KCSale_M> KCSale_M_Get(Expression<Func<KCSale_M, bool>> predicate, string sortBy, int ACID)
        {
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                try
                {
                    if (predicate == null)
                    {
                        if (string.IsNullOrWhiteSpace(sortBy))
                        {
                            return ctx.KCSale_M.ToList();
                        }
                        else
                        {
                            return ctx.KCSale_M.OrderBy(sortBy).ToList();
                        }
                    }
                    else
                    {
                        if (string.IsNullOrWhiteSpace(sortBy))
                        {
                            return ctx.KCSale_M.Where(predicate).ToList();
                        }
                        else
                        {
                            return ctx.KCSale_M.Where(predicate).OrderBy(sortBy).ToList();
                        }
                    }
                }
                catch (Exception ex)
                {
                    return null;
                }
            }
        }
        #endregion


        #region  获取KCSale_M表数据
        /// <summary>
        /// 获取KCSale_M
        /// </summary>
        /// <param name="sortBy">排序属性名</param>
        /// <param name="sortType">排序类型0=升序 1=降序</param>
        /// <returns>List<KCSale_M></returns>
        public List<KCSale_M> KCSale_M_Get(string sortBy, int ACID)
        {
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                try
                {
                    return KCSale_M_Get(null, sortBy, ACID);
                }
                catch (Exception)
                {
                    return null;
                }
            }
        }
        #endregion


        #region  获取KCSale_M表数据计数
        /// <summary>
        /// 获取KCSale_M表数据计数
        /// </summary>
        /// <returns>List<KCSale_M></returns>
        public int KCSale_M_GetCount(Expression<Func<KCSale_M, bool>> predicate, int ACID)
        {
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                try
                {
                    if (predicate == null)
                    {
                        return ctx.KCSale_M.Count();

                    }
                    else
                    {
                        return ctx.KCSale_M.Where(predicate).Count();
                    }
                }
                catch
                {
                    return 0;
                }
            }
        }
        #endregion




        #region  获取KCSale_M分页表数据
        /// <summary>
        /// 获取KCSale_M分页表数据
        /// </summary>
        /// <param name="pageIndex">当前页</param>
        /// <param name="pageSize">页大小</param>
        /// <param name="sortBy">排序属性名</param>
        /// <param name="sortType">排序类型0=升序 1=降序</param>
        /// <returns>List<KCSale_M></returns>
        public List<KCSale_M> KCSale_M_Getpage(int pageIndex, int pageSize, Expression<Func<KCSale_M, bool>> predicate, string sortBy, int ACID)
        {
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                try
                {
                    if (predicate == null)
                    {
                        if (string.IsNullOrWhiteSpace(sortBy))
                        {
                            return ctx.KCSale_M.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
                        }
                        else
                        {
                            return ctx.KCSale_M.OrderBy(sortBy).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
                        }
                    }
                    else
                    {
                        if (string.IsNullOrWhiteSpace(sortBy))
                        {
                            return ctx.KCSale_M.Where(predicate).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
                        }
                        else
                        {
                            return ctx.KCSale_M.Where(predicate).OrderBy(sortBy).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
                        }
                    }
                }
                catch (Exception ex)
                {
                    return null;
                }
            }
        }
        #endregion


        #region  批量修改KCSale_M表数据
        /// <summary>
        /// 批量修改KCSale_M
        /// </summary>
        /// <param name="predicate">Expression<Func<KCSale_M, bool>></param> 查找表达式树
        /// <param name="updater">Expression<Func<KCSale_M,KCSale_M>> 更新表达式树</param>
        /// <param name="returns">影响记录数</param>
        public int KCSale_M_batchUpdate(Expression<Func<KCSale_M, bool>> predicate, Expression<Func<KCSale_M, KCSale_M>> updater, int ACID)
        {
            using (TransactionScope ts = new TransactionScope())
            {
                using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
                {
                    try
                    {
                        int ok = ctx.KCSale_M.Update(predicate, updater);
                        ts.Complete();
                        return ok;
                    }
                    catch (Exception ex)
                    {
                        return 0;
                    }
                }
            }
        }
        #endregion


        #region  批量修改KCSale_M表数据事物用重构
        /// <summary>
        /// 批量修改KCSale_M事物用重构
        /// </summary>
        /// <param name="predicate">Expression<Func<KCSale_M, bool>></param> 查找表达式树
        /// <param name="updater">Expression<Func<KCSale_M,KCSale_M>> 更新表达式树</param>
        /// <param name="ctx">数据上下文</param>
        /// <param name="returns">影响记录数</param>
        private int KCSale_M_batchUpdate(Expression<Func<KCSale_M, bool>> predicate, Expression<Func<KCSale_M, KCSale_M>> updater, int ACID, CardsServer.DAL.DataClassesDataContext ctx)
        {
            int ok = ctx.KCSale_M.Update(predicate, updater);
            return ok;
        }
        #endregion


        #region  批量删除KCSale_M表数据
        /// <summary>
        /// 批量删除KCSale_M
        /// </summary>
        /// <param name="predicate">Expression<Func<KCSale_M, bool>></param> 查找表达式树
        /// <param name="returns">影响记录数</param>
        public int KCSale_M_batchDel(Expression<Func<KCSale_M, bool>> predicate, int ACID)
        {
            using (TransactionScope ts = new TransactionScope())
            {
                using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
                {
                    try
                    {
                        int ok = ctx.KCSale_M.Delete(predicate);
                        ts.Complete();
                        return ok;
                    }
                    catch (Exception ex)
                    {
                        return 0;
                    }
                }
            }
        }
        #endregion


        #region  批量删除KCSale_M表数据事物用重构
        /// <summary>
        /// 批量删除KCSale_M事物用重构
        /// </summary>
        /// <param name="predicate">Expression<Func<KCSale_M, bool>></param> 查找表达式树
        /// <param name="ctx">数据上下文</param>
        /// <param name="returns">影响记录数</param>
        private int KCSale_M_batchDel(Expression<Func<KCSale_M, bool>> predicate, int ACID, CardsServer.DAL.DataClassesDataContext ctx)
        {
            int ok = ctx.KCSale_M.Delete(predicate);
            return ok;
        }
        #endregion


    }
}

